package com.xiaode.controller.base;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import com.jfinal.aop.Before;
import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.core.NotAction;
import com.jfinal.core.Path;
import com.jfinal.ext.cors.EnableCORS;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.xiaode.common.constant.Const;
import com.xiaode.common.model.Admin;
import com.xiaode.common.model.Student;
import com.xiaode.common.model.UserAccount;
import com.xiaode.common.model.WlDepartment;
import com.xiaode.common.model.WlStudent;
import com.xiaode.common.model.WlTeacher;
import com.xiaode.util.apidoc.annotation.Api;
import com.xiaode.util.apidoc.annotation.ApiOper;
import com.xiaode.util.hikvision.HikvisionUtil;
import com.xiaode.util.welink.WeLinkUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;

/**
 * 基础控制器
 *
 * @author 张烨
 * @param 
 **/
@Path("/base")
@Api("基础配置服务")
@EnableCORS()
public class BaseController extends Controller {
	
	@Inject
    private BaseService service;
    
	
	
	/**
	 * 获取配置常量等基本信息
	 */
	@ApiOper("获取配置常量")
	public void getBaseInfo() throws Exception{
		Ret ret = service.getBaseInfo();
		renderJson(ret);
    }
	
	
	/**
	 * 手动同步基座用户
	 */
	@NotAction
	public void initAllUser() throws Exception{
		WeLinkUtil.initAllRole();
		WeLinkUtil.initAllUser();
		renderJson();
	}
	
	/**
	 * 手动更新一次海康摄像头
	 */
	@NotAction
	public void initHKCamera() throws Exception{
		HikvisionUtil.getAllCamera(1, 100);
		renderJson();
	}

	/**
	 * 用于部署期间用户账号和基座邦定
	 */
	@NotAction
	@Before(Tx.class)
	public void bangding() throws Exception{
		
		List<WlStudent> wsl = WlStudent.dao.findAll();
		List<WlStudent> wsnl = new ArrayList<>();
		for(WlStudent ws : wsl) {
			Student s = Student.dao.findFirst("SELECT * FROM student WHERE name = ?", ws.getUserName());
			if(BeanUtil.isEmpty(s)) {
				wsnl.add(ws);
			}else {
				s.setStudentNo(ws.getCorpUserId());
				s.update();
				UserAccount ua = UserAccount.dao.findById(s.getAccountId());
				ua.setIsBindingPedestal(true);
				ua.setEduID(ws.getUserCode());
				ua.update();
			}
		}
		System.out.println(wsnl.size());
		for(WlStudent wsn : wsnl) {
			String account = StrUtil.isBlank(wsn.getNumber())?wsn.getEmployeeId():wsn.getNumber();
			UserAccount ua = new UserAccount();
			ua.setAccount(account);
			ua.setAddTime(LocalDateTime.now());
			ua.setUserType(2);
			ua.setIsBindingPedestal(true);
			ua.setEduID(wsn.getUserCode());
			ua.save();
			ua.setPassword(SecureUtil.md5(SecureUtil.md5("111111") + ua.getId() + Const.salt));
			ua.update();
			
			Student s = new Student();
			s.setUserType(2);
			s.setStudentNo(wsn.getCorpUserId());
			s.setName(wsn.getUserName());
			s.setSex(StrUtil.equals(wsn.getSex(), "F")?2:1);
			s.setMobile(wsn.getMobileNumber());
			Integer grade = wsn.getGrade();
			if(grade == 1) {
				s.setGrade(5);
			}
			if(grade == 2) {
				s.setGrade(1);
			}
			if(grade == 3) {
				s.setGrade(2);
			}
			s.setUpdateTime(LocalDateTime.now());
			s.setNumber(wsn.getNumber());
			String deptCode = wsn.getDeptCode();
			WlDepartment wd = WlDepartment.dao.findFirst("SELECT * FROM wl_department WHERE dept_code = ?", deptCode);
			if(BeanUtil.isNotEmpty(wd)) {
				s.setClasId(wd.getOrgClassId());
			}
			s.setAddTime(LocalDateTime.now());
			s.setAccountId(ua.getId());
			s.save();
		}
		
		List<WlTeacher>  wtl = WlTeacher.dao.findAll();
		List<WlTeacher> wtnl = new ArrayList<>();
		List<WlTeacher> wtupl = new ArrayList<>();
		for(WlTeacher wt : wtl) {
			Admin a = Admin.dao.findFirst("SELECT * FROM admin WHERE name = ?", wt.getUserName());
			if(BeanUtil.isEmpty(a)) {
				wtnl.add(wt);
			}else {
				wtupl.add(wt);
				
				UserAccount ua = UserAccount.dao.findById(a.getAccountId());
				ua.setEduID(wt.getUserCode());
				ua.update();
			}
		}
		for(WlTeacher wtn : wtnl) {
			String account = wtn.getMobileNumber().split("-")[1];//手机号作为账号
			UserAccount ua = new UserAccount();
			ua.setAccount(account);
			ua.setAddTime(LocalDateTime.now());
			ua.setUserType(1);
			ua.setIsBindingPedestal(true);
			ua.setEduID(wtn.getUserCode());
			ua.save();
			ua.setPassword(SecureUtil.md5(SecureUtil.md5("111111") + ua.getId() + Const.salt));
			ua.update();
			
			Admin a = new Admin();
			a.setPhone(account);
			if(StrUtil.isNotBlank(wtn.getUserEmail())) {
				a.setEmail(wtn.getUserEmail());
			}
			a.setName(wtn.getUserName());
			a.setGender(StrUtil.equals(wtn.getSex(), "F")?2:1);
			a.setPhone(wtn.getMobileNumber());
			a.setAddTime(LocalDateTime.now());
			a.setAccountId(ua.getId());
			a.setUserType(1);
			a.save();
		}
		for(WlTeacher wtup : wtupl) {
			
			Admin a = Admin.dao.findFirst("SELECT * FROM admin WHERE name = ?", wtup.getUserName());
			a.setPhone(wtup.getMobileNumber());
			a.setEmail(wtup.getUserEmail());
			a.setGender(StrUtil.equals(wtup.getSex(), "F")?2:1);
			a.update();
			
			UserAccount ua = UserAccount.dao.findById(a.getAccountId());
			ua.setEduID(wtup.getUserCode());
			ua.update();
			
		}
		
		
		renderJson();
	
    }
	
	
	
	
	
	
	
	

}
